//
//  ViewController.m
//  FlutterMix
//
//  Created by 许龙 on 2019/3/6.
//  Copyright © 2019 qd. All rights reserved.
//

#import "ViewController.h"
#import <Flutter/Flutter.h>
#import "AppDelegate.h"
#import "NativeSecondVC.h"
#import "PushViewController.h"
#import "PresentViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIButton *presFPageBtn;
@property (nonatomic, strong) UIButton *pushFPageBtn;
@property (nonatomic, strong) UIButton *pushNPageBtn;


@property (nonatomic, strong) PresentViewController *flutterPresentVC;
@property (nonatomic, strong) FlutterMethodChannel *presentChannel;

@property (nonatomic, strong) PushViewController *flutterPushVC;
@property (nonatomic, strong) FlutterMethodChannel *pushChannel;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"原生";
    self.view.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.presFPageBtn];
    self.presFPageBtn.frame = CGRectMake(100, 200, 180, 48);
    
    [self.view addSubview:self.pushFPageBtn];
    self.pushFPageBtn.frame = CGRectMake(100, 300, 180, 48);
    
    [self.view addSubview:self.pushNPageBtn];
    self.pushNPageBtn.frame = CGRectMake(100, 400, 180, 48);
}

- (NSString *)getDeiveName {
    UIDevice *device = UIDevice.currentDevice;
    return device.name;
}

- (void)presentFlutterPage:(UIButton *)sender {
//    FlutterEngine *flutterEngine = [(AppDelegate *)[[UIApplication sharedApplication] delegate] flutterEngine];
    self.flutterPresentVC = [[PresentViewController alloc] init];
    self.flutterPresentVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self.flutterPresentVC setInitialRoute:@"presentPage"];
    [self presentViewController:self.flutterPresentVC animated:false completion:nil];
    
    self.presentChannel = [FlutterMethodChannel methodChannelWithName:@"willian.flutter.io/willian_present" binaryMessenger:self.flutterPresentVC];
    
    __weak typeof(self) weakSelf = self;
    // 注册方法等待flutter页面调用
    [self.presentChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
        NSLog(@"%@", call.method);
        NSLog(@"%@", result);
        
        if ([call.method isEqualToString:@"getNativeResult"]) {
            NSString *name = [weakSelf getDeiveName];
            if (name == nil) {
                FlutterError *error = [FlutterError errorWithCode:@"UNAVAILABLE" message:@"Device info unavailable" details:nil];
                result(error);
            } else {
                result(name);
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                // 原生调用Flutter方法，带参数, 接收回传结果
                [weakSelf queryFlutterInfor];
            });
        } else if ([call.method isEqualToString:@"dismiss"]) {
            [weakSelf.flutterPresentVC dismissViewControllerAnimated:YES completion:nil];
        } else if ([call.method isEqualToString:@"presentNativeSecondPage"]) {
            NativeSecondVC *secondVC = [[NativeSecondVC alloc] init];
            secondVC.modalPresentationStyle = UIModalPresentationFullScreen;
            [self.flutterPresentVC presentViewController:secondVC animated:YES completion:nil];
        } else {
            result(FlutterMethodNotImplemented);
        }
    }];
}

- (void)queryFlutterInfor{
    [self.presentChannel invokeMethod:@"flutterMedia" arguments:@{@"key1": @"value1"} result:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [self showAlert:result];
    }];
}

- (void)pushFlutterPage:(UIButton *)sender {
//    FlutterEngine *flutterEngine = [(AppDelegate *)[[UIApplication sharedApplication] delegate] flutterEngine];
    self.flutterPushVC = [[PushViewController alloc] init];
    [self.flutterPushVC setInitialRoute:@"pushPage"];
    [self.navigationController pushViewController:self.flutterPushVC animated:true];
}


- (void)pushNPageBtnClick:(UIButton *)sender {
    NativeSecondVC *secondVC = [[NativeSecondVC alloc] init];
    [self.navigationController pushViewController:secondVC animated:YES];
}


- (void)showAlert:(NSString *)message {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Flutter返回" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
    [alertVC addAction:defaultAction];
    [self.flutterPresentVC presentViewController:alertVC animated:YES completion:nil];
}

- (UIButton *)presFPageBtn{
    if(!_presFPageBtn){
        _presFPageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_presFPageBtn setTitle:@"present flutter page" forState:UIControlStateNormal];
        [_presFPageBtn addTarget:self action:@selector(presentFlutterPage:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _presFPageBtn;
}

- (UIButton *)pushFPageBtn{
    if(!_pushFPageBtn){
        _pushFPageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_pushFPageBtn setTitle:@"push flutte page" forState:UIControlStateNormal];
        [_pushFPageBtn addTarget:self action:@selector(pushFlutterPage:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pushFPageBtn;
}

- (UIButton *)pushNPageBtn{
    if(!_pushNPageBtn){
        _pushNPageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_pushNPageBtn setTitle:@"push native page" forState:UIControlStateNormal];
        [_pushNPageBtn addTarget:self action:@selector(pushNPageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pushNPageBtn;
}

@end
